//
//  XDPlaceDetailsRegionViewController.m
//  Le way walkers
//
//  Created by YaZhiLiang on 15/5/16.
//  Copyright (c) 2015年 LTXZ. All rights reserved.
//

#import "XDPlaceDetailsRegionViewController.h"

#import "XDPlaceDetailsRegionCollectionViewCell.h"

#import "XDPhotoTextViewController.h"

#import "XDPlaceDetailsRegionCollectionViewCell.h"

#import "NetworkHandle.h"

#import "UIImageView+WebCache.h"

#import "XDPlaceCityModel.h"

#import "MJRefresh.h"

@interface XDPlaceDetailsRegionViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UIImageView *gifImageView;

@property (nonatomic, retain) UIView *gifView;

@property (nonatomic, assign) NSInteger num;

@property (nonatomic, assign) NSInteger numinfo;

@end

@implementation XDPlaceDetailsRegionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.Arr = [NSMutableArray array];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
        self.navigationController.navigationBar.alpha = 1;
    
    [self createSubViews];
    
    [self yangTuo];
    
    [self.collectionView addFooterWithCallback:^{
        
        [self getData];
    }];

}

- (void)viewWillAppear:(BOOL)animated
{
    
    self.navigationController.navigationBar.alpha = 1;
    
    self.navigationController.navigationBar.hidden = NO;
    
    self.navigationController.navigationBarHidden = NO;
    
    self.tabBarController.tabBar.hidden = NO;
    
}

- (void)createSubViews
{
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"iconfont-xiangzuo"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(back:)];
    
    self.title = self.titleName;
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    
    flowLayout.itemSize = CGSizeMake((self.view.frame.size.width - 50) / 3, (self.view.frame.size.width - 50) / 3);
    
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
//    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    UIColor *image1 = [UIColor colorWithPatternImage:[UIImage imageNamed:@"0EWIF4R2T0S6.jpg"]];
    
    self.collectionView.backgroundColor = image1;
    
    self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, 113, 0);
    
    self.collectionView.dataSource = self;
    
    self.collectionView.delegate = self;
    
    [self.view addSubview:self.collectionView];
    
    [flowLayout release];
    
    [_collectionView release];
    
    
    [self.collectionView registerClass:[XDPlaceDetailsRegionCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

}

- (void)back:(UIBarButtonItem *)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)getData
{
    if (self.num >= 1) {
        
        self.numinfo = self.num * 18;
    }
    
    [NetworkHandle getDataWithURL:[NSString stringWithFormat:@"http://api.breadtrip.com//destination/place/%@/%@/photos/?gallery_mode=1&count=18&start=%ld&sign=609765cf9fe4ec1b49c71497401a566", self.typeName, self.ScenicID, self.numinfo] completion:^(id result) {
       
        NSArray *arr = [result objectForKey:@"items"];

        
        for (NSDictionary *dic in arr) {
            
            XDPlaceCityModel *cityModel = [[XDPlaceCityModel alloc]initWithDic:dic];
            
            [self.Arr addObject:cityModel];
            
            [cityModel release];
            
        }
        
        [self.collectionView reloadData];
        
        [self.gifImageView stopAnimating];
        
        self.gifView.hidden = YES;
        
    }];
    
    self.num++;
    [self.collectionView footerEndRefreshing];
    
}


- (void)yangTuo
{
        [self getData];
    
    self.gifImageView.hidden = NO;
    self.gifImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 40, self.view.frame.size.height / 2 - 50, 80, 100)];
    NSArray *gifArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"cao1.png"],
                         [UIImage imageNamed:@"cao2.png"],
                         [UIImage imageNamed:@"cao3.png"],
                         [UIImage imageNamed:@"cao4.png"],
                         [UIImage imageNamed:@"cao5.png"],
                         [UIImage imageNamed:@"cao6.png"],
                         [UIImage imageNamed:@"cao7.png"],
                         [UIImage imageNamed:@"cao8.png"],
                         [UIImage imageNamed:@"cao9.png"],
                         [UIImage imageNamed:@"cao10.png"],nil];
    self.gifImageView.animationImages = gifArray; //动画图片数组
    self.gifImageView.animationDuration = 0.8; //执行一次完整动画所需的时长
    self.gifImageView.animationRepeatCount = 10000;  //动画重复次数
    self.gifView = [[UIView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    UIColor *imageB = [UIColor colorWithPatternImage:[UIImage imageNamed:@"123456_123456"]];
    self.gifView.backgroundColor = imageB;
    [self.gifView addSubview:self.gifImageView];
    [self.gifImageView startAnimating];
    [self.view addSubview:self.gifView];
    [_gifImageView release];
    [_gifView release];;
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSLog(@"---------- $$ %ld", self.Arr.count);
    return self.Arr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    XDPlaceDetailsRegionCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    XDPlaceCityModel *xdplaceCity = self.Arr[indexPath.row];
    
    cell.xdPlaceCityModel = xdplaceCity;
    
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    XDPhotoTextViewController *XDTextVC = [[[XDPhotoTextViewController alloc]init] autorelease];
    
    self.num_photo = indexPath.row;
    
    XDTextVC.number_photo = self.num_photo;
    
    XDTextVC.photo_text_details = self.Arr;
    
    [self.navigationController pushViewController:XDTextVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
